/**
Copyright (c) 2013 cnova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

library JButton requires JWidget

    private function MakeImage takes string path, real x, real y, real size returns image
        return CreateImage(path, size, size, size, x, y, 0.0, 0, 0, 0, 2)
    endfunction
    //! runtextmacro define_sparse_array("BUTTONS")

    interface ClickListener
        method onClick takes JButton source returns nothing defaults nothing
    endinterface
    
    struct JButton extends JWidget
    
        private static constant integer unittypeid = 'hfoo'
        private static constant integer pid = 11
    
        private static trigger T = null
        
        private unit face = null
        private image icon = null
        private real size = 128.0
        private string iconPath = null
        
        private HashSet listeners

        public static method getInstance takes unit face returns thistype
            return BUTTONS[GetHandleId(GetTriggerUnit())]
        endmethod
        private static method HandleButtonClick takes nothing returns boolean
            local thistype this = BUTTONS[GetHandleId(GetTriggerUnit())]
            local Iterator it
            local ClickListener listener
            if this != 0 then
                set it  = .listeners.iterator()
                loop
                    exitwhen it.end
                    set listener = it.next()
                    call listener.onClick.execute(this)
                endloop
            endif
            return false
        endmethod
        method addClickListener takes ClickListener listener returns nothing
            call .listeners.add(listener)
        endmethod
        method removeClickListener takes ClickListener listener returns nothing
            call .listeners.remove(listener)
        endmethod
        
        static method create takes nothing returns thistype
            local thistype this = .allocate()
            local integer i = 0
            
            set .face = CreateUnit(Player(thistype.pid), .unittypeid, 0, 0, 270.0)
            call SetUnitVertexColor(.face, 255,255,255,1)
            call PauseUnit(.face, true)
            call SetUnitInvulnerable(.face,true)
            call SetUnitPathing(.face,true)
            call SetUnitX(.face,0.0)
            call SetUnitY(.face,0.0)
            
            if thistype.T == null then
                set thistype.T = CreateTrigger()
                loop
                    exitwhen i >  13
                    call TriggerRegisterPlayerUnitEvent(                        /*
                            */ thistype.T,                                      /*
                            */ Player(i),                                       /*
                            */ EVENT_PLAYER_UNIT_SELECTED,                      /*
                            */ null  /*
                            */ )
                    set i = i + 1
                endloop
                call TriggerAddCondition(thistype.T, Condition (function thistype.HandleButtonClick))
            endif

            set .listeners = HashSet.create()
            set BUTTONS[GetHandleId(.face)] = this
            return this
        endmethod
        method operator texture= takes string path returns nothing
            set .iconPath = path
            if path != null then
                if .icon != null and GetHandleId(.icon) > 0 then
                    call DestroyImage(.icon)
                endif
                set .icon = MakeImage(path, .x-.size/2.0,.y-.size/2.0, .size)
                if .icon != null and GetHandleId(.icon) >0 and .visible then
                    call SetImageRenderAlways(.icon, true)
                else
                    call BJDebugMsg("Error creating image: JButton.texture= iconPath: " +.iconPath)
                endif
            endif
        endmethod
        method setSize takes integer size returns nothing
            set .size = size
            call SetUnitScale(.face, (I2R(size))/256.0, (I2R(size))/256.0, (I2R(size))/256.0)
            set .texture = .iconPath
        endmethod
        stub method draw takes nothing returns nothing
            if .face != null then
                call ShowUnit(.face, true)
                call SetUnitX(.face,.x)
                call SetUnitY(.face,.y)
            endif
            if .icon != null and GetHandleId(.icon) >0 then
                call SetImageRenderAlways(.icon, true)
                call SetImagePosition(.icon,.x-.size/2,.y-.size/2,0.0)
            endif
        endmethod
        stub method hide takes nothing returns nothing
            if .face != null then
                call ShowUnit(.face, false)
            endif
            if .icon != null and GetHandleId(.icon) >0 then
                call SetImageRenderAlways(.icon, false)
            endif
        endmethod
        
        method onDestroy takes nothing returns nothing
            if .face != null then
                set BUTTONS[GetHandleId(.face)] = 0
                call RemoveUnit(.face)
            endif
            if .icon != null and GetHandleId(.icon) >0 then
                call DestroyImage(.icon)
            endif
            call .listeners.destroy()
        endmethod
    endstruct
endlibrary